package com.woniuxy.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.entity.Message;
import com.woniuxy.entity.Orders;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.MessageService;
import com.woniuxy.service.OrdersService;
import com.woniuxy.util.CommonUtil;
/**
 * 
 * @author lishuwei&zenglingzhong
 *
 */
@Controller
@RequestMapping("/msg")
public class MsgController {
	@Autowired
	private MessageService messageService;
	@Autowired
	private OrdersService ordersService;

	
	@RequestMapping("/show")
	@ResponseBody
	public ResponseEntity<PageBean<Message>> getUnReadMsg(int page){
		ResponseEntity<PageBean<Message>> re = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
//		User nowUser = new User().setId(7);
		try {
			PageBean<Message> pb = messageService.getUnreadByPage(nowUser.getId(), "2", page);
			re.setData(pb);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	
	@RequestMapping("/mark")
	@ResponseBody
	public ResponseEntity<Void> markToReaded(int id){
		ResponseEntity<Void> re = new ResponseEntity<>();
		try {
			messageService.markToReaded(id);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	
	@RequestMapping("/new")
	@ResponseBody
	public ResponseEntity<Integer> newMsg(){
		ResponseEntity<Integer> re = new ResponseEntity<>();
		try {
			User nowUser  = CommonUtil.getCurrentUser();
			int num = messageService.getUnreadedNum(nowUser.getId());
			re.setCode(1);
			re.setData(num);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	
	@RequestMapping("/acceptOrder")
	@ResponseBody
	public ResponseEntity<Void> acceptOrder(Integer oid,Integer mid){
		ResponseEntity<Void> re = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
		try {
			int res = messageService.handleOrder(oid,"2",nowUser.getRole());
			if(res==3) {
				re.setCode(3);//表示该申请已被处理
				markToReaded(mid);
				return re;
			}
			markToReaded(mid);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		
		return re;
	}
	
	/**
	 * 
	 * @param oid
	 * @param mid
	 * @return 1成功，2其他异常，3账户余额不足，4订单支付
	 */
	@RequestMapping("/pay")
	@ResponseBody
	public ResponseEntity<Void> payOrder(Integer oid,Integer mid){
		ResponseEntity<Void> re = new ResponseEntity<>();
		try {
			int res = messageService.payOrder(oid);
			if(res==3) {
				re.setCode(3);
				return re;
			}
			if(res==4) {
				re.setCode(4);
				if(mid!=0) {
					markToReaded(mid);
				}
				return re;
			}
			markToReaded(mid);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		
		return re;
	}
	
	@RequestMapping("/acceptSign")
	@ResponseBody
	public ResponseEntity<Void> acceptSign(Integer sid,Integer mid){
		ResponseEntity<Void> re = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
		try {
			messageService.handleSign(sid,"3",nowUser.getRole());
			markToReaded(mid);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		
		return re;
	}
	
	@RequestMapping("/refuseOrder")
	@ResponseBody
	public ResponseEntity<Void> refuseOrder(Integer oid,Integer mid){
		User nowUser  = CommonUtil.getCurrentUser();
		ResponseEntity<Void> re = new ResponseEntity<>();
		try {
			Orders order=ordersService.getOrdersByOid(oid);
			if (order.getUserIdGym()!=0) {
				ordersService.releaseCourse(order.getChooseId());
				
			}
			int res = messageService.handleOrder(oid,"4",nowUser.getRole());
			if(res==3) {
				re.setCode(3);
				return re;
			}
			markToReaded(mid);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	@RequestMapping("/refuseSign")
	@ResponseBody
	public ResponseEntity<Void> refuseSign(Integer sid,Integer mid){
		ResponseEntity<Void> re = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
		try {
			messageService.handleSign(sid,"4",nowUser.getRole());
			markToReaded(mid);
			re.setCode(1);
			} catch (Exception e) {
				e.printStackTrace();
				re.setCode(2);
			}
			return re;
		}
	
	@RequestMapping("/out")
	@ResponseBody
	public ResponseEntity<Void> outMoneyReq(int id,double num){
		ResponseEntity<Void> re = new ResponseEntity<>();
		if(num<=0) {
			re.setCode(3);
			return re;
		}
		try {
			boolean res = messageService.outMoneyReq(id,num);
			if(!res) {
				re.setCode(4);
				return re;
			}

			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
}
